package com.walkline.dianping.dao;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.dianping.DianpingException;
import com.walkline.dianping.DianpingSDK;
import com.walkline.dianping.inf.DealResult;
import com.walkline.dianping.inf.SingleDeal;

public class DianpingDealResult extends DianpingObject implements DealResult
{
	private SingleDeal[] _deals;

	public DianpingDealResult(DianpingSDK dianping, JSONObject jsonObject) throws DianpingException
	{
		super(dianping, jsonObject);

		JSONArray dealArray = _jsonObject.optJSONArray("deals");

		_deals = new SingleDeal[dealArray.length()];

		for (int i=0; i<dealArray.length(); i++)
		{
			JSONObject deal;
			try {
				deal = dealArray.getJSONObject(i);

				if (deal != null)
				{
					_deals[i] = new DianpingSingleDeal(dianping, deal);
				}
			} catch (JSONException e) {}
		}
	}

	//本次API访问所获取的单页团购数量
	public int getCounts() {return _jsonObject.optInt("count");}

	//本次API访问状态，如果成功返回"OK"，并返回结果字段，如果失败返回"ERROR"，并返回错误说明
	public String getStatus() {return _jsonObject.optString("status");}

	//团购详细信息集合
	public SingleDeal[] getDeals() {return _deals;}
}